#!/usr/bin/env python
# encoding: utf-8
"""
NewGameFrame.py

Created by Steve Streza on 2008-09-24.
Copyright (c) 2008 __MyCompanyName__. All rights reserved.
"""

import sys
import string
import os
from Tkinter import *

class NewGameFrame(Frame):
     """frame to allow the user to make a new game. it is inserted into
        the GameInputView. callsback after input is collected
     """
     
     def __init__(self,master,doneHandler,**kv):
          """constructor. doneHandler is the callback method"""
          Frame.__init__(self,master,kv)
          
          self.doneHandler = doneHandler
          
          self.makerLabel   = Label(self, text="Maker: ")
          self.breakerLabel = Label(self, text="Breaker: ")
          self.logLabel     = Label(self, text="Log: ")
          self.delayLabel   = Label(self, text="Delay: ")
          
          self.breakerValue = IntVar()       
          self.humanBreakerRadio = Radiobutton(self, text="Human", variable=self.breakerValue, value=0)
          self.cpuBreakerRadio = Radiobutton(self, text="Computer", variable=self.breakerValue, value=1, command=self.success)
          self.humanBreakerRadio.select()
          self.makerValue = IntVar()         
          self.humanMakerRadio = Radiobutton(self, text="Human",    variable=self.makerValue, value=0)
          self.humanMakerRadio.select()
          #self.cpuMakerRadio   = Radiobutton(self, text="Computer", variable=self.makerValue, value=1)
          
          self.logEntry = Entry(self)
          self.delayEntry = Entry(self)
          
          self.doneButton = Button(self, text="New Game", command=self.handleDone)

#         self.makerLabel.grid(row=0, column=0)
#         self.humanMakerRadio.grid(row=0, column=1)
#         self.cpuMakerRadio.grid(row=0, column=2)
          
          self.breakerLabel.grid(row=1, column=0)
          self.humanBreakerRadio.grid(row=1, column=1)
          self.cpuBreakerRadio.grid(row=1, column=2)
          
          self.logLabel.grid(row=2, column=0)
          self.logEntry.grid(row=2, column=1, columnspan=2)
          
          self.delayLabel.grid(row=3,column=0)
          self.delayEntry.grid(row=3,column=1)
          self.doneButton.grid(row=3,column=2)
     
     def handleDone(self):
          """callback"""
          waitTimeStr = self.delayEntry.get()
          waitTime = 0
          if waitTimeStr != "":
               waitTime = string.atoi(waitTimeStr,10)
          
          maker = self.makerValue.get()
          breaker = self.breakerValue.get()
          if breaker == 0:
               waitTime = 0
          logPath = self.logEntry.get()
          if logPath == "":
               logPath = None
          
          handler = self.doneHandler
          handler(waitTime, maker, breaker, logPath)

     def success(self):
          """mark as successful"""
          self.breakerValue.set(1)
          

        
                
